const app = getApp();
var {Api} = require('../../api/api');
const network = require("../../api/network.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mask:false,
    id:'',//题目id
    page:1,
    page_size:20,
    items:[],//评论列表
    content:'',//评论内容
    length:0,//评论的字数
    p_id:'',//上级id
    kong:false,//是否为空
    domain:Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('xxxxx','xxx')
    this.setData({
      id:options.id
    })
    this.init()
  },
  mask(e){
    let {id} = e.currentTarget.dataset
    this.setData({
      mask:true,
      p_id:id
    })
  },
  noMask(){
    this.setData({
      mask:false
    })
  },
  init(){//评论列表
    var params = {
      subject_id:this.data.id,
      member_id:wx.$cache.get('member_id'),//用户id
      page:1,
      page_size:this.data.page_size,
    }
    wx.$http.get(wx.$get.comment_list,params).then(res=>{
      console.log(res,'评论列表')
      if(res.length==0){
        this.setData({
          kong:true
        })
      }else{
        this.setData({
          kong:false
        })
      }
      this.setData({
        items:res
      })
    })
  },
  bindcontent(e){
    this.setData({
      content:e.detail.value
    })
    this.setData({
      length:this.data.content.length
    })
  },
  btn(){//提交评论
    var that = this
    var params = {
      subject_id:this.data.id,
      p_id:this.data.p_id?this.data.p_id:'0',
      content:this.data.content,
      one_id:this.data.p_id?this.data.p_id:'0'
    }
    wx.$http.get(wx.$get.comment_add,params).then(res=>{
      console.log(res,'提交评论')
      that.init()
      that.noMask()
      that.setData({
        content:''
      })
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 2];
      if(thisPage.route=="pages/brushTopic/brushTopic"){
        thisPage.tidetail2()
      }
    })
  },
  zhan(e){//评论点赞
    const {id} = e.currentTarget.dataset
    const {index} = e.currentTarget.dataset
    const {give} = e.currentTarget.dataset
    var that = this
    var params = {
      log_id:id,
      type:2
    }
    wx.$http.get(wx.$get.give_log,params).then(res=>{
      console.log(res,'点赞')
      if(give==1){
        that.setData({
          [`items[${index}].give_num`]:that.data.items[index].give_num-=1,
          [`items[${index}].is_give`]:2
        })
      }else{
        that.setData({
          [`items[${index}].give_num`]:that.data.items[index].give_num+=1,
          [`items[${index}].is_give`]:1
        })
      }
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 2];
      if(thisPage.route=="pages/brushTopic/brushTopic"){
        thisPage.tidetail2()
      }
    })
  },
  pulldown(){//评论列表上拉加载
    var that = this
    this.setData({
      page:this.data.page+=1
    })
    var params = {
      subject_id:this.data.id,
      member_id:network.get('userinfo').id,//用户id
      page:this.data.page,
      page_size:this.data.page_size,
    }
    wx.$http.get(wx.$get.comment_list,params).then(res=>{
      console.log(res,'评论列表')
      if(res.length!=0){
        that.setData({
          items:[...this.data.items,...res]
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pulldown()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:network.get('fx').title,
      imageUrl:network.get('fx').img,
      path:`/pages/index/index?pid=${network.get('member_id')}`
    }
  }
})